<?php




namespace App\Http\Controllers\Api\User;


use App\Http\Controllers\Api\BasicController;
use App\Models\SiteCode;
use App\Models\User;
use App\Models\UserSocialite;
use App\Services\Sms;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;
use Jiannei\Response\Laravel\Support\Facades\Response;
use Shopwwi\WebmanSocialite\Facade\Socialite;

class AliPayController extends BasicController
{

    /**
     * @api {post} /user/alipay/app 支付宝APP登陆
     * @apiName UserWechatAPP
     * @apiGroup UserWechat
     * @apiDescription 微信APP登陆
     *
     * @apiParam {String} avatar
     * @apiParam {String} openid
     * @apiParam {String} nickname
     */
    public function app()
    {
        $data = $this->data(['avatar', 'openid', 'nickname']);

        $social = (new UserSocialite())->getSocialite($data['openid'], UserSocialite::WECHAT, $data);
        if ($social->uid > 0) {
            $user = (new User())->getDetail($social->uid);
            $token = (new User())->token($user);
            return Response::success($token);
        } else {
            return Response::success($social);
        }
    }

    public function mobile()
    {
        $data = $this->data(['id', 'mobile', 'code']);
        $where['mobile'] = $data['mobile'];
        (new Sms())->check($data['mobile'], $data['code'], SiteCode::CODE_CHANGE);
        $user = (new User())->getUser($where);
        if (empty($user)) {
            $insert['mobile'] = $data['mobile'];
            $user = (new User())->createUser($insert);
        }
        (new UserSocialite())->updateUid($data['id'], $user->uid);
        $token = (new User())->token($user);
        return Response::success($token);
    }

    public function login(Request $request){
        if (!$request->has('auth_code')) {
            echo '获取app_auth_code失败,请重试';
            exit;
        }
        //获取app_auth_token
        $ali_pay_login_cg = config('alipay')['login'];
        $base_param = array(
            'app_id'     => $ali_pay_login_cg['app_id'],
            'grant_type' => $ali_pay_login_cg['grant_type'],
            'charset'    => $ali_pay_login_cg['charset'],
            'sign_type'  => $ali_pay_login_cg['sign_type'],
            'version'    => $ali_pay_login_cg['version'],
        );
        $get_token_param = array_merge($base_param, array(
            'method'     => $ali_pay_login_cg['get_token_api'],
            'code'       => $request->auth_code,
            'timestamp'  => date('Y-m-d H:i:s')
        ));
        $signStr = aliPayParamToString($get_token_param);        //参数拼接
        $rsa = $this->enRSA2($signStr);                                 //对拼接好的请求参数数据进行RSA加密
        $sign = urlencode($rsa);                                 //生成url格式的请求参数
        $query = $signStr . '&sign=' . $sign;                    //请求参数
        $url = $ali_pay_login_cg['base_url'] . $query;
        $access_token = getHttpResponsePOST($ali_pay_login_cg['base_url'], $query);
        $access_token_info = json_decode($access_token, true);
        if (!isset($access_token_info['alipay_system_oauth_token_response'])) {


            echo '获取token信息失败';
            exit;
        }
        $access_token = $access_token_info['alipay_system_oauth_token_response']['access_token'];
        //请求用户信息
        $get_info_param = array_merge($base_param, array(
            'method'     => $ali_pay_login_cg['user_info_api'],
            'timestamp'  => date('Y-m-d H:i:s'),
            'auth_token' => $access_token,
        ));
        $signStr = aliPayParamToString($get_info_param);//拼接参数
        $rsaStr = enRSA2($signStr);                     //RSA加密
        $sign = urlencode($rsaStr);                     //生成url格式的请求参数
        $query = $signStr . '&sign=' . $sign;//请求参数
        $user_info = getHttpResponsePOST($ali_pay_login_cg['base_url'], $query);
        $user_info = mb_convert_encoding($user_info, 'utf-8', 'gbk');//把返回结果，从gbk格式转为utf-8格式
        $user_info = json_decode($user_info, true);                  //解析json
        //接下来，写自己的业务逻辑
    }


}
